<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Symbol.for和Symbol.keyFor</title>
</head>
<body>
    <h1>Symbol.for()和Symbol.keyFor()</h1>
    <h2>Symbol.for()会在全局注册表中搜索键为该字符串的Symbol值</h2>

    <script>
        // Symbol.for()
        let s1 = Symbol.for('foo');
        let s2 = Symbol.for('foo');
        console.log(s1 === s2); // true

        // Symbol()
        let s3 = Symbol('foo');
        let s4 = Symbol('foo');
        console.log(s3 === s4); // false

        // Symbol.keyFor()
        let foo = Symbol.for("foo");
        console.log(Symbol.keyFor(foo)); // "foo"

        let bar = Symbol("bar");
        console.log(Symbol.keyFor(bar)); // undefined
    </script>
</body>
</html> 